//
//  NoIDViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/2.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "NoIDViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface NoIDViewController ()
@property(nonatomic,copy)UIView *backGroundView;
@property(nonatomic,assign)CGRect originFrame;
@end

@implementation NoIDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createUI
{
    //背景图片
    UIImageView *backGroundView=[[UIImageView alloc]initWithFrame:self.view.bounds];
    backGroundView.userInteractionEnabled=YES;
    backGroundView.image=[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:@"loginBackground-6.png" ofType:nil]];
    [self.view addSubview:backGroundView];
    
    
    //返回按钮
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(0, 20, 40, 40);
    backBtn.tag=1;
    [backBtn setBackgroundImage:[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:@"baritem_left_hl-1@2x.png" ofType:nil]] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:backBtn];
    
    //背景视图
    _backGroundView=[[UIView alloc]init];
    
    
    //用户名
    UITextField *userTextField=[[UITextField alloc]initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-20*2, 50)];
    
    UIView *userLeftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    
    UIImageView *userLeftImgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:@"userName-1-1.png" ofType:nil]]];
    userLeftImgView.frame=CGRectMake(20, (50-47*0.6)/2.0, 41*0.6, 47*0.6);
    [userLeftView addSubview:userLeftImgView];
    
    
    
    
    
    userTextField.leftView=userLeftView;
    userTextField.leftViewMode=UITextFieldViewModeAlways;
    userTextField.backgroundColor=[UIColor whiteColor];
    userTextField.layer.borderColor=[UIColor colorWithRed:238 green:232 blue:236 alpha:1].CGColor;
    userTextField.layer.borderWidth=1.0;
    userTextField.layer.cornerRadius=5.0;
    [_backGroundView addSubview:userTextField];
    
    
    //密码
    UITextField *passwordTextField=[[UITextField alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(userTextField.frame)+10, self.view.frame.size.width-20*2, 50)];
    
    UIView *passwordLeftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    
    UIImageView *passworLeftImgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:@"password-3-1.png" ofType:nil]]];
    passworLeftImgView.frame=CGRectMake(20, (50-47*0.6)/2.0, 41*0.6, 47*0.6);
    [passwordLeftView addSubview:passworLeftImgView];
    
    
    passwordTextField.leftView=passwordLeftView;
    passwordTextField.leftViewMode=UITextFieldViewModeAlways;
    passwordTextField.backgroundColor=[UIColor whiteColor];
    passwordTextField.layer.borderColor=[UIColor colorWithRed:238 green:232 blue:236 alpha:1].CGColor;
    passwordTextField.layer.borderWidth=1.0;
    passwordTextField.layer.cornerRadius=5.0;
    passwordTextField.secureTextEntry=YES;
    passwordTextField.keyboardType=UIKeyboardTypeNamePhonePad;
    [_backGroundView addSubview:passwordTextField];
    
    
    //监听
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardIsShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardIsHidden:) name:UIKeyboardDidHideNotification object:nil];
    
    _backGroundView.frame=CGRectMake(0, self.view.frame.size.height/2.0, self.view.frame.size.width, userTextField.frame.size.height*2+10);
    [backGroundView addSubview:_backGroundView];
    _originFrame=_backGroundView.frame;
    
    
    CGFloat btn_w=self.view.frame.size.width/3.0;
    CGFloat btn_h=35;
    //马上登陆
    UIButton *loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame=CGRectMake(btn_w, CGRectGetMaxY(_backGroundView.frame)+20, btn_w, btn_h);
    loginBtn.backgroundColor=[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];
    [loginBtn setTitle:@"马上登陆" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    loginBtn.tag=2;
    loginBtn.layer.borderColor=[UIColor colorWithRed:238 green:232 blue:236 alpha:1].CGColor;
    loginBtn.layer.borderWidth=1.0;
    loginBtn.layer.cornerRadius=5.0;
    [loginBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:loginBtn];
    
    //忘记密码
    UIButton *forgotPasswordBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    forgotPasswordBtn.frame=CGRectMake(btn_w, CGRectGetMaxY(loginBtn.frame)+10, btn_w, btn_h);
    forgotPasswordBtn.backgroundColor=[UIColor colorWithRed:250/255.0 green:250/255.0 blue:251/255.0 alpha:1];
    [forgotPasswordBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgotPasswordBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    forgotPasswordBtn.tag=3;
    forgotPasswordBtn.layer.borderColor=[UIColor colorWithRed:238 green:232 blue:236 alpha:1].CGColor;
    forgotPasswordBtn.layer.borderWidth=1.0;
    forgotPasswordBtn.layer.cornerRadius=5.0;
    [forgotPasswordBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:forgotPasswordBtn];

    //用户注册
    UIButton *registerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    registerBtn.frame=CGRectMake(btn_w, CGRectGetMaxY(forgotPasswordBtn.frame)+10, btn_w, btn_h);
    registerBtn.backgroundColor=[UIColor colorWithRed:250/255.0 green:250/255.0 blue:251/255.0 alpha:1];
    [registerBtn setTitle:@"用户注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    registerBtn.tag=4;
    registerBtn.layer.borderColor=[UIColor colorWithRed:238 green:232 blue:236 alpha:1].CGColor;
    registerBtn.layer.borderWidth=1.0;
    registerBtn.layer.cornerRadius=5.0;
    [registerBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:registerBtn];
}

#pragma mark 按钮响应
-(void)btnClick:(UIButton *)sender
{
    if (sender.tag==1) {
        //返回上一页
        self.navigationController.tabBarController.tabBar.hidden=NO;
        AppDelegate *app=[[UIApplication sharedApplication]delegate];
        app.vc.selectedIndex=app.tabbarItemTag;
    }
}
//-(void)backToViewController
//{
////    AppDelegate *app=[[UIApplication sharedApplication]delegate];
////    NSLog(@"------------------%ld",app.tabbarItemTag);
////    if (app.tabbarItemTag==1) {
//        [self.navigationController popToRootViewControllerAnimated:YES];
////    }else if (app.tabbarItemTag==2){
////        ProductViewController *vc=[[ProductViewController alloc]init];
////        [self.navigationController popToViewController:vc animated:YES];
////    }else if (app.tabbarItemTag==3){
////        CommunityViewController *vc=[[CommunityViewController alloc]init];
////        [self.navigationController popToViewController:vc animated:YES];
////    }else if (app.tabbarItemTag==4) {
////        HomeViewController *vc=[[HomeViewController alloc]init];
////        [self.navigationController popToViewController:vc animated:YES];
////    }
//}
#pragma mark 显示键盘
-(void)keyBoardIsShow:(NSNotification *)notify
{
    CGRect currentFrame = _backGroundView.frame;
    CGRect kbFrame=[[notify.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    currentFrame.origin.y=kbFrame.origin.y-currentFrame.size.height;
    _backGroundView.frame=currentFrame;
    NSLog(@"%@",notify);
//    [UIView animateWithDuration:0.01 animations:^{
//       _backGroundView.frame=currentFrame;
//    }];
}

#pragma mark 隐藏键盘
-(void)keyBoardIsHidden:(NSNotification *)notify
{
    _backGroundView.frame=_originFrame;
//    [UIView animateWithDuration:0.01 animations:^{
//        _backGroundView.frame=_originFrame;
//    }];
//    CGRect currentFrame = _backGroundView.frame;
//    CGRect kbFrame=[[notify.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
//    currentFrame.origin.y=kbFrame.origin.y-currentFrame.size.height;
//    _backGroundView.frame=currentFrame;
//    NSLog(@"%@",notify);
}

#pragma mark  计算键盘的高度
-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo
{
    CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    CGRect keyboardEndingFrame = [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil];
    return keyboardEndingFrame.size.height;
}
#pragma mark 关闭键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.navigationController.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
